using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace MLTimeline.Editor
{
    public class TimelineEditorWindow : EditorWindow
    {

        [MenuItem("工具/MLTimeline Editor")]
        public static void ShowWindow()
        {
            TimelineEditorWindow wnd = GetWindow<TimelineEditorWindow>();
            wnd.titleContent = new GUIContent("MLTimeline Editor");
        }

        private void CreateGUI()
        {
            var loader = TimelineEditorLoader.Instance;
            if (loader == null)
            {
                Debug.LogWarning("当前加载器不存在。");
                return;
            }
            if (loader.timelineEditor == null)
            {
                Debug.LogWarning("当前窗口uxml文件不存在，请检查下，是否将uxml文件拖入TimelineEditorLoader.cs文件的Inspector面板内。");
                return;
            }
            if (loader.timelineEditorStyle == null)
            {
                Debug.LogWarning("当前窗口uss文件不存在，请检查下，是否将uss文件拖入TimelineEditorLoader.cs文件的Inspector面板内。");
                return;
            }
            // 1. 加载UXML布局文件
            loader.timelineEditor.CloneTree(rootVisualElement);

            // 2. 加载USS样式文件
            rootVisualElement.styleSheets.Add(loader.timelineEditorStyle);
            // 3. 在这里进行绑定
            FindAndBindUI();
        }

        private void FindAndBindUI()
        {
            var timelineViewPanel = rootVisualElement.Q("timeline-view-panel");
            timelineViewPanel.Add(new TimelineView());
        }

    }
}